// ContainerPage.cpp : implementation file
//

#include "stdafx.h"
#include "txt_pqdif.h"
#include "ContainerPage.h"
#include "AppStr.h"
#include "ipqd_time.h"
#include "names.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static const int AUTHOR_BUF_LEN = 129;
static const int TITLE_BUF_LEN = 129;
static const int NOTES_BUF_LEN = 513;

/////////////////////////////////////////////////////////////////////////////
// CContainerPage property page

IMPLEMENT_DYNCREATE(CContainerPage, CPropertyPage)

CContainerPage::CContainerPage() : CPropertyPage(CContainerPage::IDD)
{
	//{{AFX_DATA_INIT(CContainerPage)
	//}}AFX_DATA_INIT
}

CContainerPage::~CContainerPage()
{
}

void CContainerPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_EDIT_AUTHOR, m_edtAuthor);
	DDX_Control(pDX, IDC_EDIT_NOTES, m_edtNotes);
	DDX_Control(pDX, IDC_EDIT_TITLE, m_edtTitle);
	DDX_Control(pDX, IDC_EDIT_TITLE2, m_edtLanguage);
	DDX_Control(pDX, IDC_EDIT_VERSION, m_edtVersion);
	DDX_Control(pDX, IDC_EDIT_TITLE3, m_edtApplication);
	DDX_Control(pDX, IDC_EDIT_TITLE4, m_edtTrademarks);
	DDX_Control(pDX, IDC_EDIT_AUTHOR3, m_edtLastSaveBy);
	DDX_Control(pDX, IDC_EDIT_COMMENT, m_edtComments);
	DDX_Control(pDX, IDC_EDIT_NOTES4, m_edtCopyright);
	DDX_Control(pDX, IDC_EDIT_NOTES3, m_edtSecurity);
}


BEGIN_MESSAGE_MAP(CContainerPage, CPropertyPage)
	//{{AFX_MSG_MAP(CContainerPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CContainerPage message handlers

BOOL CContainerPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	m_edtAuthor.SetLimitText (AUTHOR_BUF_LEN - 1);
	m_edtNotes.SetLimitText (NOTES_BUF_LEN - 1);
	m_edtTitle.SetLimitText (TITLE_BUF_LEN - 1);

	
	return TRUE;
}

void CContainerPage::TransferFromContainer(MCFContainerInfo* pCI )
{
	CString csTitle,csLanguage,csAuthor,csNotes,csApplication,csTrademarks;
	CString csLastSaveBy,csComments,csCopyright,csSecurity,csVersion;
	APPSTR_ASCII tranW_Title(pCI->szTitle);
	APPSTR_ASCII tranW_Language(pCI->szLanguage);
	APPSTR_ASCII tranW_Author(pCI->szAuthor);
	APPSTR_ASCII tranW_Notes(pCI->szNotes);
	APPSTR_ASCII tranW_Application(pCI->szApplication);
	APPSTR_ASCII tranW_Trademarks(pCI->szTrademarks);
	APPSTR_ASCII tranW_LastSaveBy(pCI->szLastSaveBy);
	APPSTR_ASCII tranW_Comments(pCI->szComments);
	APPSTR_ASCII tranW_Copyright(pCI->szCopyright);
	APPSTR_ASCII tranW_Security(pCI->szSecurity);

	csTitle = tranW_Title.WCHAR_PTR();
	csLanguage = tranW_Language.WCHAR_PTR();
	csAuthor = tranW_Author.WCHAR_PTR();
	csNotes = tranW_Notes.WCHAR_PTR();

	csApplication = tranW_Application.WCHAR_PTR();
	csTrademarks = tranW_Trademarks.WCHAR_PTR();
	csLastSaveBy = tranW_LastSaveBy.WCHAR_PTR();
	csComments = tranW_Comments.WCHAR_PTR();
	csCopyright = tranW_Copyright.WCHAR_PTR();
	csSecurity = tranW_Security.WCHAR_PTR();

	csVersion.Format(_T("%u"),pCI->u4VersionInfo);
	

	m_edtTitle.SetWindowText(csTitle);
	m_edtLanguage.SetWindowText(csLanguage);
	m_edtAuthor.SetWindowText(csAuthor);
	m_edtNotes.SetWindowText(csNotes);
	m_edtApplication.SetWindowText(csApplication);
	m_edtTrademarks.SetWindowText(csTrademarks);
	m_edtLastSaveBy.SetWindowText(csLastSaveBy);
	m_edtComments.SetWindowText(csComments);
	m_edtCopyright.SetWindowText(csCopyright);
	m_edtSecurity.SetWindowText(csSecurity);

	m_edtVersion.SetWindowText(csVersion);

	createTime = pCI->timestampCreation;
}

void CContainerPage::TransferToContainer( MCFContainerInfo* pCI )
{
	CString cont_Title,cont_Language,cont_Author,cont_Notes,cont_Application,cont_Trademarks;
	CString cont_LastSaveBy,cont_Comments,cont_Copyright,cont_Security,cont_Version;;
	m_edtTitle.GetWindowText(cont_Title);
	m_edtLanguage.GetWindowText(cont_Language);
	m_edtAuthor.GetWindowText(cont_Author);
	m_edtNotes.GetWindowText(cont_Notes);

	m_edtApplication.GetWindowText(cont_Application);
	m_edtTrademarks.GetWindowText(cont_Trademarks);
	m_edtLastSaveBy.GetWindowText(cont_LastSaveBy);
	m_edtComments.GetWindowText(cont_Comments);
	m_edtCopyright.GetWindowText(cont_Copyright);
	m_edtSecurity.GetWindowText(cont_Security);

	m_edtVersion.GetWindowText(cont_Version);

	WideCharToMultiByte( CP_OEMCP, NULL, cont_Title, -1, 
		pCI->szTitle, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );
	WideCharToMultiByte( CP_OEMCP, NULL, cont_Language, -1, 
		pCI->szLanguage, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );
	WideCharToMultiByte( CP_OEMCP, NULL, cont_Author, -1, 
		pCI->szAuthor, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );
	WideCharToMultiByte( CP_OEMCP, NULL, cont_Notes, -1, 
		pCI->szNotes, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );

	WideCharToMultiByte( CP_OEMCP, NULL, cont_Application, -1, 
		pCI->szApplication, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );
	WideCharToMultiByte( CP_OEMCP, NULL, cont_Trademarks, -1, 
		pCI->szTrademarks, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );
	WideCharToMultiByte( CP_OEMCP, NULL, cont_LastSaveBy, -1, 
		pCI->szLastSaveBy, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );
	WideCharToMultiByte( CP_OEMCP, NULL, cont_Comments, -1, 
		pCI->szComments, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );

	WideCharToMultiByte( CP_OEMCP, NULL, cont_Copyright, -1, 
		pCI->szCopyright, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );
	WideCharToMultiByte( CP_OEMCP, NULL, cont_Security, -1, 
		pCI->szSecurity, MCFIPQ_EXTERNNAME_LENGTH, NULL, FALSE );

	pCI->u4VersionInfo = _tcstoul(cont_Version,NULL,10);

	SYSTEMTIME sysTime;
	GetLocalTime(&sysTime);
	TIMESTAMPPQDIF tsNow = St2TsPQDIF(&sysTime);
	pCI->timestampLastSaved = tsNow;
	pCI->u4TimesSaved++;

	if (createTime.day == 0)
	{
		pCI->timestampCreation = tsNow;
	}
}

void CContainerPage::ClearAllInfo()
{
	m_edtAuthor.Clear();
	m_edtNotes.Clear();
	m_edtTitle.Clear();
	m_edtLanguage.Clear();
	m_edtVersion.Clear();
	m_edtApplication.Clear();
	m_edtTrademarks.Clear();
	m_edtLastSaveBy.Clear();
	m_edtComments.Clear();
	m_edtCopyright.Clear();
	m_edtSecurity.Clear();
}
